import { defineStore } from "pinia";
import { UserState } from "@/stores/interface";
import piniaPersistConfig from "@/config/piniaPersist";

export const useUserStore = defineStore({
  id: "geeker-user",
  state: (): UserState => ({
    accessToken: "",
    refreshToken: "",
    userInfo: {
      name: "",
      userAvatar: "src/assets/images/avatar.gif",
      userId: 0,
      departmentId: 0,
      userRole: 0,
      departmentName: "用户部门"
    }
  }),
  getters: {},
  actions: {
    // Set Token
    setAccessToken(token: string) {
      this.accessToken = token;
    },
    setRefreshToken(token: string) {
      this.refreshToken = token;
    },
    // Set setUserInfo
    setUserInfo(userInfo: UserState["userInfo"]) {
      if (userInfo.name == undefined) {
        userInfo.name = "undefined";
      }
      if (userInfo.userAvatar == undefined) {
        userInfo.userAvatar = "src/assets/images/avatar.gif";
      }
      this.userInfo = userInfo;
    }
  },
  persist: piniaPersistConfig("geeker-user")
});
